package com.park.controller;

import com.park.common.constant.IDBConstant;
import com.park.common.poo.OtherResource;
import com.park.common.util.FileUploader;
import com.park.common.util.FileUtil;
import com.park.common.util.StrUtil;
import com.park.platform.service.IResourceService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RequestMapping("cms")
@Controller
public class CmsController extends BaseController {

    private static Logger logger = Logger.getLogger(CmsController.class);

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private FileUploader fileUploader;

    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Map<String, Object> upload(MultipartHttpServletRequest multipartRequest, HttpServletResponse response) throws Exception {
        Map map = new HashMap();
        try {
            if (multipartRequest != null) {
                for (Iterator<String> it = multipartRequest.getFileNames(); it.hasNext(); ) {
                    String key = it.next();
                    MultipartFile multipartFile = multipartRequest.getFile(key);
                    String originalFilename = multipartFile.getOriginalFilename();
                    String name = multipartFile.getName();
                    logger.info("originalFilename==>" + originalFilename + " name==>" + name);

                    /*if (!PictureValidateUtil.isPicture(originalFilename)) {
                        map.put("result", "不是图片");
                        map.put("status", false);
                        AjaxJsonWriteGson(map, true, response);
                        return map;
                    }*/
                    if (originalFilename.length() > 0) {
                        //写资源到数据库
                        ////////////////////////////
                        OtherResource resource = resourceService.saveRedactorResource(multipartFile.getOriginalFilename(), new Long(multipartFile.getSize()).intValue(), -1, IDBConstant.RESOURCE_SERVICE_TYPE_CMS_ARTICLE, IDBConstant.RESOURCE_TYPE_IMG);
                        resourceService.save(resource);
                                                                                    //fileUploader.getCmsImagesFilePath()：D:/zleida/zleida-www/src/main/webapp/pages/cms/images
                        fileUploader.saveToRootDisc(multipartFile.getInputStream(), FileUtil.getUploadPath(), resource.getResourceName());

                        //获得资源地址

                                        //fileUploader.getCmsImagesAccessURL()：http://localhost:8085/pages/cms/images/
                        map.put("filelink", FileUtil.getImgHttp(resource.getResourceName()));
                        if(StrUtil.getMimeType(resource.getResourceName()) == null) {
                            map.put("type", "video");
                        }

                        return map;
                        //AjaxJsonWriteGson(map, true, response);
                    }
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return map;
    }

}
